//
//  TGMCollectionViewEmptyCell.m
//  TravelGuideHD
//
//  Created by xiaofeng on 14-9-2.
//  Copyright (c) 2014年 mafengo Inc. All rights reserved.
//

#import "TGMCollectionViewEmptyCell.h"
#import "TGMActivityIndicatorView.h"

@interface TGMCollectionViewEmptyCell()
{
    
}
@property(nonatomic,assign)float tipsStateDownY;

@end

@implementation TGMCollectionViewEmptyCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _activityView = [TGMActivityIndicatorView defaultActivityIndicatorView];
        [_activityView stopAnimating];
        _activityView.hidden = YES;
        [self.contentView addSubview:_activityView];
        
        _tipsImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_tipsImageView];
        
        _tipsLabelView = [[UILabel alloc] initWithFrame:CGRectZero];
        _tipsLabelView.font = [UIFont systemFontOfSize:13];
        _tipsLabelView.textAlignment = NSTextAlignmentCenter;
        _tipsLabelView.numberOfLines = 0;
//        _tipsLabelView.textColor = SECOND_TEXT_COLOR;
        _tipsLabelView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_tipsLabelView];
        
        self.contentView.backgroundColor = [UIColor clearColor];
        
//        self.state = EmptyCellState_Loading;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (self.state == EmptyCellState_Tips || self.state == EmptyCellState_Error)
    {
        _tipsImageView.hidden = NO;
        _tipsLabelView.hidden = NO;
        
        [_activityView stopAnimating];
        
        _tipsLabelView.text = self.emptyText;
        
        UIImage *image = self.emptyImage;
        
        if (image == nil)
        {
            if (self.state == EmptyCellState_Tips)
            {
                NSInteger random = arc4random() % 5;
                image = [UIImage imageNamed:[NSString stringWithFormat:@"empty_cell_%ld",random]];
                if (self.emptyText == nil)
                {
                    NSArray *textArray = [NSArray arrayWithObjects:EMPTY_TEXT_0, EMPTY_TEXT_1, EMPTY_TEXT_2, EMPTY_TEXT_3, EMPTY_TEXT_4, nil];
                    _tipsLabelView.text = textArray[random];
                }
            }
            else
            {
                image = [UIImage imageNamed:@"re_load"];
            }
            
        }
        _tipsImageView.image = image;
        
        NSDictionary *attributes = @{NSFontAttributeName: _tipsLabelView.font};
        CGSize tipsTxtSize = [_tipsLabelView.text boundingRectWithSize:CGSizeMake(self.width - 20, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
        int tipsContentHeight = image.size.height;
        if ((int)tipsTxtSize.height > 0)
        {
            tipsContentHeight +=  IMAGE_TEXT_GAP + tipsTxtSize.height;
        }
        
        if(self.state == EmptyCellState_Tips){
            int startX = 0;
            int startY = (self.height - tipsContentHeight)/2;
            if(self.tipsStateDownY > 0.0f){
                startY = startY+self.tipsStateDownY;
            }
            startX = (self.width - image.size.width) * .5;
            _tipsImageView.frame = CGRectMake(startX, startY, image.size.width, image.size.height);
            
            startY += image.size.height + IMAGE_TEXT_GAP;
            startX = 0;
            _tipsLabelView.frame = CGRectMake(10, startY, self.width - 20, tipsTxtSize.height);
        }else{
            int startX = 0;
            int startY = (self.frame.size.height - tipsContentHeight) * 0.5;
            
            startX = (self.width - image.size.width) * .5;
            _tipsImageView.frame = CGRectMake(startX, startY, image.size.width, image.size.height);
            
            startY += image.size.height + tipsContentHeight;
            startX = 0;
            _tipsLabelView.frame = CGRectMake(10, startY, self.width - 20, tipsTxtSize.height);
        }
    }
    else
    {
        _tipsImageView.hidden = YES;
        _tipsLabelView.hidden = YES;
        
        [_activityView startAnimating];
        _activityView.center = CGPointMake(self.frame.size.width * .5, self.frame.size.height * .5);
    }
    
}




-(void)tipsDownY:(float)y{
    self.tipsStateDownY = y;
}

@end
